package com.woniuxy01;

import java.io.*;
import java.util.Arrays;

/**
 * @author 作者
 * @create 2025-08-01 11:35
 */
public class Test04 {
    public static void main(String[] args) {
        try (FileInputStream fis = new FileInputStream("a.txt");) {
            byte[] bytes = new byte[2];
            int length = 0;
            while ((length = fis.read(bytes)) != -1) {
                System.out.println(new String(bytes, 0, length));
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private static void test08() {
        FileInputStream fis = null;
        try {
            fis = new FileInputStream("a.txt");
            byte[] bytes = new byte[2];
            int length = 0;
            while ((length = fis.read(bytes)) != -1) {
                System.out.println(new String(bytes, 0, length));
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (fis != null) {
                try {
                    fis.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }
    }

    private static void test07() throws IOException {
        FileInputStream fis = new FileInputStream("a.txt");
        //一次读取一个字节数组，数组的长度一般使用1024的整数倍
        //public int read(byte b[]):读取的数据在数组中,返回值代表读取了几个字节
        byte[] bytes = new byte[2];
        int length = 0;
        while ((length = fis.read(bytes)) != -1) {
            System.out.println(new String(bytes, 0, length));
        }
        fis.close();
    }

    private static void test06() throws IOException {
        //读取文件FileInputStream
        FileInputStream fis = new FileInputStream("a.txt");
        //一次读取一个字节,public int read():返回值就是读取的字节的编码，-1文件读完了
        int mybyte = 0;
        while ((mybyte = fis.read()) != -1) {
            System.out.println(mybyte);
            System.out.println((char) mybyte);
        }
        fis.close();
    }

    private static void test05() {
        //try-with-resources
        //try(定义需要关闭资源的代码，不需要写finally,代码最后会自动执行close方法)
        try (FileOutputStream fos = new FileOutputStream("a.txt");) {
            fos.write("abvc".getBytes());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private static void test04() {
        FileOutputStream fos = null;
        try {
            fos = new FileOutputStream("x:\\a.txt");
            fos.write("abvc".getBytes());
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (fos != null) {
                    fos.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        System.out.println("over");
    }

    private static void test03() throws IOException {
        FileOutputStream fos = new FileOutputStream("b.txt");
        String str = "abcdefg中文";
        //一次写一个字节数组
        fos.write(str.getBytes());
        fos.close();
    }

    private static void test02() throws IOException {
        //没有指定路径，使用相对路径：当前项目所在的目录
        FileOutputStream fos = new FileOutputStream("b.txt");
        byte[] bytes = {'a', 'b', 'c', 99};
        //一次写一个字节数组
        fos.write(bytes);
        fos.close();
    }

    private static void test01() throws IOException {
        //字节流，写数据
//        FileOutputStream fos = new FileOutputStream(new File("d:\\aaa\\a.txt"));
        FileOutputStream fos = new FileOutputStream("d:\\aaa\\a.txt");
        //写数据：一次写一个字节
        fos.write(97);
//        fos.write('A');
        //IO流使用结束以后，手动关闭
        fos.close();
    }
}
